package com.easonji.admin.service.impl;

import com.easonji.admin.manager.UserManager;
import com.easonji.admin.model.UserUserPost;
import com.easonji.admin.mapper.UserUserPostMapper;
import com.easonji.admin.service.IUserUserPostService;
import com.easonji.common.base.service.AbstractBaseService;
import com.easonji.common.util.JsonUtils;
import com.easonji.common.page.PageInfo;
import com.easonji.common.util.StringUtils;
import com.easonji.common.base.query.BaseQuery;
import com.easonji.common.factory.PageFactory;

import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import com.alibaba.fastjson.JSONObject;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 员工上下级关系及职务管理 服务实现类
 * </p>
 *
 * @author easonji
 * @since 2018-10-01
 */
@Service
public class UserUserPostServiceImpl extends AbstractBaseService<UserUserPost> implements IUserUserPostService {

    @Resource
    private UserUserPostMapper userUserPostMapper;

    @Resource
    private UserManager userManager;

    @Override
    protected JSONObject warp2Json(UserUserPost userUserPost) {
       JSONObject result = JsonUtils.parseObject(userUserPost);
       result.put("sUserName",userManager.getUserNameById(userUserPost.getsUser()));
       return result;
    }


    @Override
    public Object queryList(BaseQuery baseQuery){

        // 分页信息
        Page<UserUserPost> page = new PageFactory<UserUserPost>().defaultPage();
        // 查询条件
        Wrapper<UserUserPost> wrapper = new EntityWrapper<>();

        List<UserUserPost> userUserPosts = userUserPostMapper.selectPage(page, wrapper);

        return new PageInfo(warp(userUserPosts), page.getTotal());
    }

    @Override
    public Object queryRowById(Integer sId) {
        UserUserPost userUserPost = userUserPostMapper.selectById(sId);
        return warp(userUserPost);
    }

    @Override
    public Integer save(UserUserPost userUserPost) {

        return userUserPostMapper.insert(userUserPost);
    }

    @Override
    public Integer update(UserUserPost userUserPostForm) {
        UserUserPost userUserPost = new UserUserPost();
        BeanUtils.copyProperties(userUserPostForm,userUserPost);
        return userUserPostMapper.updateById(userUserPost);
    }

    @Override
    public Integer delete(Integer sId) {
        Wrapper<UserUserPost> wrapper = new EntityWrapper<>();
        wrapper.eq("s_id", sId);
        return userUserPostMapper.delete(wrapper);
    }

    @Override
    public Integer queryUserPidByuserIdandpost(Integer userId,Integer postId){
        Integer userpId = null;

        Wrapper<UserUserPost> wrapper = new EntityWrapper<>();
        wrapper.eq("s_user",userId);
        wrapper.eq("s_post",postId);
        List<UserUserPost> userUserPosts = userUserPostMapper.selectList(wrapper);
        if(StringUtils.isNotEmpty(userUserPosts)){
            userpId = userUserPosts.get(0).getsPUser();
        }
        return userpId;

    }

    @Override
    public Object queryListByPid(Integer pid){

        // 查询条件
        Wrapper<UserUserPost> wrapper = new EntityWrapper<>();

        wrapper.eq("s_p_user",pid);

        List<UserUserPost> userUserPosts = userUserPostMapper.selectList( wrapper);

        return warp(userUserPosts);
    }

}
